package com.ruoyi.system.FabricBussinessContract;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.system.BusinessContract.domain.BusinessContract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/fabricContract")
public class FabricBussinessContractController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    // 获取合同信息
    @GetMapping("/{contractId}")
    public BusinessContract getContractById(@PathVariable String contractId) throws JsonProcessingException {
        String url = "http://localhost:8082/BussinessContractFabric/" + contractId;  // 假设8082端口是Fabric系统
        Map<String, Object> response = restTemplate.getForObject(url, Map.class);
        String payloadJson = (String) response.get("payload"); // 获取JSON字符串
        BussinessContractDTO dto = objectMapper.readValue(payloadJson, BussinessContractDTO.class);

        // 将返回的 DTO 转换为业务系统中的 BusinessContract
        return ContractFabricToBussinessMapper.toBusinessContract(dto);
    }

    // 创建合同
    @PutMapping("/create/")
    public Map<String, Object> createContract(@RequestBody BusinessContract contract) throws JsonProcessingException {
        String url = "http://localhost:8082/BussinessContractFabric/create/";

        // 将业务系统的 BusinessContract 转换为智能合约系统需要的 BussinessContractDTO
        BussinessContractDTO dto = ContractFabricToBussinessMapper.toDTO(contract);
        // 将 DTO 转换为 JSON 字符串
        String dtoJson = objectMapper.writeValueAsString(dto);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建 HttpEntity
        HttpEntity<String> request = new HttpEntity<>(dtoJson, headers);

        // 调用 Fabric 系统的接口
        return restTemplate.postForObject(url, request, Map.class);
    }

    // 更新合同
    @PostMapping("/update/")
    public Map<String, Object> updateContract(@RequestBody BusinessContract contract) throws JsonProcessingException {
        String url = "http://localhost:8082/BussinessContractFabric/update/";

        // 将业务系统的 BusinessContract 转换为智能合约系统需要的 BussinessContractDTO
        BussinessContractDTO dto = ContractFabricToBussinessMapper.toDTO(contract);
        // 将 DTO 转换为 JSON 字符串
        String dtoJson = objectMapper.writeValueAsString(dto);
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 创建 HttpEntity
        HttpEntity<String> request = new HttpEntity<>(dtoJson, headers);

        // 调用 Fabric 系统的接口
        return restTemplate.postForObject(url, request, Map.class);
    }

    // 删除合同
    @DeleteMapping("/delete/{contractId}")
    public void deleteContract(@PathVariable String contractId) {
        String url = "http://localhost:8082/BussinessContractFabric/delete/" + contractId;
        restTemplate.delete(url);
    }

    // 按货主和合同类型查询合同
    @GetMapping("/owner/{owner}/type/{contractType}")
    public BusinessContract queryContractByOwnerAndType(@PathVariable String owner, @PathVariable String contractType) throws JsonProcessingException {
        String url = "http://localhost:8082/BussinessContractFabric/owner/" + owner + "/type/" + contractType;
        Map<String, Object> response = restTemplate.getForObject(url, Map.class);
        String payloadJson = (String) response.get("payload"); // 获取JSON字符串
        // 使用Jackson手动反序列化
        BussinessContractDTO dto = objectMapper.readValue(payloadJson, BussinessContractDTO.class);
        // 将 BusinessContract 转换为 JSON 字符串
        return ContractFabricToBussinessMapper.toBusinessContract(dto);
    }
}
